//
//  CXBaseViewController.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/11/30.
//

import UIKit

class CXBaseViewController: UIViewController, UIGestureRecognizerDelegate{
    /**
     导航栏父视图
     */
    lazy var navigationCXView: UIView = UIView()
    
    /**
     导航标题
     */
    lazy var titleNavLabel: UILabel = UILabel()
    
    /**
     返回按钮
     */
    lazy var backNavButton: UIButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //控制器背景颜色
        self.view.backgroundColor = UIColor.white
        
        //设置状态栏的字体颜色
        UIApplication.shared.statusBarStyle = .lightContent
        
        //开启系统边缘侧滑返回，要遵守代理UIGestureRecognizerDelegate
        navigationController?.interactivePopGestureRecognizer?.delegate = self
        
        //创建导航栏
        createCXNav()
        
    }
    
    // 这个方法是在手势将要激活前调用
    // 返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    //创建导航
    func createCXNav(){
        //导航栏
        navigationCXView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        navigationCXView.isHidden = false//不隐藏
        navigationCXView.backgroundColor = kMainColor_NavBG
        view.addSubview(navigationCXView)
        
        //创建返回键
        backNavButton.frame = CGRect(x: 0, y: MC_StatusBarHeight, width: 64, height: 44)
        backNavButton.contentMode = .scaleAspectFill
        backNavButton.clipsToBounds = true
        backNavButton.setImage(UIImage(named: "m_back"), for: .normal)
        backNavButton.addTarget(self, action: #selector(pressButtonLeft), for: .touchUpInside)
        navigationCXView.addSubview(backNavButton)
        
        //导航栏标题
        titleNavLabel.frame = CGRect(x: 44, y: MC_StatusBarHeight, width: SCREEN_WIDTH-88, height: 44)
        titleNavLabel.backgroundColor = .clear
        titleNavLabel.font = .boldSystemFont(ofSize: 18)
        titleNavLabel.textColor = .black
        titleNavLabel.text = ""
        titleNavLabel.textAlignment = .center
        navigationCXView.addSubview(titleNavLabel)
        
        //分割线
        let lineNavView = UIView()
        lineNavView.frame = CGRect(x: 0, y: MC_NavHeight-0.5, width: SCREEN_WIDTH, height: 0.5)
        lineNavView.backgroundColor = .groupTableViewBackground
        navigationCXView.addSubview(lineNavView)
        
    }
    
    //导航栏返回点击事件
    @objc func pressButtonLeft(){
        let viewcontrollers:[UIViewController] = self.navigationController!.viewControllers
        if viewcontrollers.count>1 {
            if viewcontrollers.last == self {
                //push方式
                self.navigationController?.popViewController(animated: true)
            }
        }else{
            //present方式
            self.dismiss(animated: true, completion: nil)
        }
        
    }
    
    //将要出现
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //影藏导航栏
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        
    }
    //已经出现
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    //将要消失
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //关闭键盘
        self.view.endEditing(true)
    }
    
    deinit {
        print("控制器销毁了")
    }
    
    //侧换返回和点击返回按钮都会走这里，控制不走dealloc也会走这里。
    override func didMove(toParent parent: UIViewController?) {
        super.didMove(toParent: parent)
        if parent == nil {
            print("页面pop成功了")
            //销毁通知
            NotificationCenter.default.removeObserver(self)
        }
    }

  

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
